package model

import (
	"gorm.io/gorm"
	__ "grpc/basic/proto"
)

// 视频表
type Video struct {
	gorm.Model
	UserId     int    `gorm:"type:int" json:"userId"`
	Title      string `gorm:"type:varchar(20)" json:"title"`      //标题
	Content    string `gorm:"type:varchar(50)" json:"content"`    //简介
	Image      string `gorm:"type:varchar(255)" json:"image"`     //封面
	Types      string `gorm:"type:varchar(10)" json:"types"`      //类型
	Permission string `gorm:"type:varchar(10)" json:"permission"` //观看权限
}

func (v *Video) AddVideo(db *gorm.DB) error {
	return db.Debug().Create(&v).Error
}

func (v *Video) ListVideo(db *gorm.DB, cond string) (list []*__.ListVideo, err error) {
	err = db.Debug().Model(&Video{}).Select("videos.id,title,content,image,types,permission").
		Where("types =?", cond).Find(&list).Error
	return list, err
}

func (v *Video) VideoList(db *gorm.DB) (list []*__.VideoList, err error) {
	err = db.Debug().Model(&Video{}).
		Select("videos.id as videoId", "users.username as username", "videos.title as title",
			"videos.content as content", "videos.image as image", "videos.types as types", "videos.permission as permission",
			"reviews.status as status", "ratings.fraction as fraction", "actors.actor_name as actorName",
			"actors.make as make", "actors.director as director",
			"videos.created_at as created").
		Joins("left join users on users.id = videos.user_id").
		Joins("left join ratings on ratings.video_id = videos.id").
		Joins("left join actors on actors.video_id = videos.id").
		Joins("left join reviews on reviews.video_id = videos.id").
		Find(&list).Error
	return list, err
}

// 评分表
type Rating struct {
	gorm.Model
	UserId   int    `gorm:"type:int" json:"userId"`
	VideoId  int    `gorm:"type:int" json:"videoId"`
	Fraction string `gorm:"type:varchar(10)" json:"fraction"`
}

type Actor struct {
	gorm.Model
	VideoId   int    `gorm:"type:int" json:"videoId"`
	ActorName string `gorm:"type:varchar(10)" json:"actorName"`
	Director  string `gorm:"type:varchar(10)" json:"director"` //导演
	Make      string `gorm:"type:varchar(20)" json:"make"`     //制作公司
}
